package com.kedacom.emptyproject.util

import android.view.animation.AccelerateDecelerateInterpolator

/**
 * @author GuanHaoran
 * @description:
 * @date :2020/10/21 1:42 PM
 */
class MeshHelper {
    private var width = 0f
    private var height = 0f
    private var bitmapWidth = 0f
    private var bitmapHeight = 0f

    private val WIDTH_DET = 40
    private var HEIGHT_DET: Int = 40


    private var startX: Float = 0f
    private var endX: Float = 0f
    private var startY: Float = 0f
    private var endY: Float = 0f

    //核心移动曲线
    private val mInterpolator = AccelerateDecelerateInterpolator()
    fun init(width: Int, height: Int) {
        this.width = width.toFloat()
        this.height = height.toFloat()
    }

    fun setBitmapDet(mapWidth: Int, mapHeight: Int) {
//        this.bitmapWidth = width;
//        this.bitmapHeight = width/mapWidth * mapHeight;
        bitmapWidth = mapWidth.toFloat()
        bitmapHeight = mapHeight.toFloat()
    }

    fun setOffset(startX: Float, endX: Float, startY: Float, endY: Float) {
        this.startX = startX
        this.endX = endX
        this.startY = startY
        this.endY = endY
    }

    fun getStartX(): Float = startX
    fun getEndX(): Float = endX
    fun getStartY(): Float = startY
    fun getEndY(): Float = endY

    lateinit var leftLine: LinePosi
    lateinit var rightLine: LinePosi
    var offset = 0f
    var destY = 0f
    lateinit var newFloat: FloatArray
    var num = 0
    var posiY = 0f
    var leftPosiX = 0f
    var rightPosiX = 0f
    var disPosiX = 0f
    var posiX = 0f
    fun setPosi(posi: Float): FloatArray? {
        //左右边线运动轨迹

        offset = height - bitmapHeight - startY

        //在0~0.3f的部分,左右轨迹要逐渐向中心靠拢
        if (posi <= 0.5f) {
//            leftLine = new LinePosi(0,width*0.1f* (posi/0.3f),0,height);
            leftLine =
                LinePosi(
                    startX,
                    endX * 0.9f * (posi / 0.5f) + (startX * (1 - posi)),
                    startY,
                    endY,
                    1
                )
            //            rightLine = new LinePosi(width,width*0.2f+width*0.8f*(0.3f-posi)/0.3f,0,height);
            rightLine = LinePosi(
                endX - (startX * (1 - posi)),
                (endX * 0.9f + width * 0.1f * (0.5f - posi) / 0.5f) - (startX * (1 - posi)),
                startY,
                endY,
                2
            )
            //            Log.e("qin","posi = "+posi+" fromX = " + width+" -> " + (width*0.9f+width*0.1f*(0.3f-posi)/0.3f) + "fromY = " +(height - bitmapHeight) + " -> " + height/2);
        } else {
            //在0.3f~1f,左右轨迹保持不变,图像按照此轨迹作为边界进行运动
            leftLine = LinePosi(startX, endX * 0.9f, startY, endY, 1)
            rightLine = LinePosi(endX, endX * 0.9f, startY, endY, 2)
            //            Log.e("qin","posi = "+posi+" fromX = " + width+" -> " + (width*0.9f+width*0.1f*(0.3f-posi)/0.3f) + "fromY = " +(height - bitmapHeight) + " -> " + height/2);
        }
        //        float destY = height * posi;
        destY = (startY - endY) * posi
        newFloat = FloatArray((WIDTH_DET + 1) * (HEIGHT_DET + 1) * 2)
        num = 0
        for (i in 0..HEIGHT_DET) {
            for (j in 0..WIDTH_DET) {

                //Y轴点位根据实际bitmap高度进行平均
//                float posiY = destY + bitmapHeight * i / HEIGHT_DET;
                posiY = startY - destY + bitmapHeight * i / HEIGHT_DET * (1 - posi)
                leftPosiX = leftLine.inputY(posiY)
                rightPosiX = rightLine.inputY(posiY)
                disPosiX = rightPosiX - leftPosiX

                //X点位根据两个line在Y值时的差值进行平均分布
                posiX = disPosiX * j / WIDTH_DET + leftPosiX
                //                float posiX = j /WIDTH_DET * bitmapWidth *posi + leftPosiX;

//                Log.e("qin","disPosiX = "+disPosiX +"  rightPosiX = " + rightPosiX +"  leftPosiX = "+leftPosiX);
//                Log.e("qin","posiY = "+posiY +"**" + "  leftPosiX = " + leftPosiX );

                //先X后Y输出
                newFloat[num] = posiX
                num++
                newFloat[num] = posiY
                num++
            }
        }
        return newFloat
    }

    fun getVetWidth(): Int {
        return WIDTH_DET
    }

    fun getVetHeight(): Int {
        return HEIGHT_DET
    }

    /**
     * 描述运动轨迹(起点XY和终点XY以及变函数Interpolator),根据输入量,可输出相对应的值
     */
    /* private class LinePosi{
        private float fromX,toX,fromY,toY;
        LinePosi(float fromX,float toX,float fromY,float toY){
            this.fromX = fromX;
            this.fromY = fromY;
            this.toX = toX;
            this.toY = toY;
        }

        public float inputY(float yP){
            float disLength = toY - fromY;
            float disXLength = toX - fromX;
            float disFloat = yP / disLength;
            float disXFloat = mInterpolator.getInterpolation(disFloat);
            float merage = disXLength * disXFloat;
            return fromX + merage;
        }
    }
*/
    /**
     * 描述运动轨迹(起点XY和终点XY以及变函数Interpolator),根据输入量,可输出相对应的值
     */
    /* private class LinePosi{
        private float fromX,toX,fromY,toY;
        LinePosi(float fromX,float toX,float fromY,float toY){
            this.fromX = fromX;
            this.fromY = fromY;
            this.toX = toX;
            this.toY = toY;
        }

        public float inputY(float yP){
            float disLength = toY - fromY;
            float disXLength = toX - fromX;
            float disFloat = yP / disLength;
            float disXFloat = mInterpolator.getInterpolation(disFloat);
            float merage = disXLength * disXFloat;
            return fromX + merage;
        }
    }
*/
    /**
     * 描述运动轨迹(起点XY和终点XY以及变函数Interpolator),根据输入量,可输出相对应的值
     */
    inner class LinePosi internal constructor(
        private val fromX: Float,
        private val toX: Float,
        private val fromY: Float,
        private val toY: Float,
        private val type: Int
    ) {
        fun inputY(yP: Float): Float {
            val disYLength = fromY - toY //Y轴的总间隔
            val disXLength = toX - fromX //X轴的总间隔
            val disFloat: Float = yP / endY // 当前Y轴距离 / Y轴总间隔
            val disXFloat: Float =
                mInterpolator.getInterpolation(Math.abs(disFloat))
            val merage = disXLength * disXFloat
            /* if (type == 1) Log.e(
                 "qin",
                 "yP = $yP  disYLength = $disYLength  disXLength = $disXLength  disFloat = " + Math.abs(
                     disFloat
                 ) + "  disXFloat = " + disXFloat + "  merage = " + merage
             )*/
            return fromX + merage
        }

    }
}

